package com.attendance.www.hr.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import com.attendance.www.hr.beans.ReportAttendanceBean;
import com.attendance.www.hr.beans.ReportAttendancePageBean;
import com.attendance.www.hr.beans.ReportAttendanceParam;
import com.attendance.www.hr.beans.SalaryBean;
import com.attendance.www.hr.beans.SalaryListBean;
import com.attendance.www.hr.beans.SalaryPageBean;
import com.attendance.www.hr.constant.AppConstant;
import com.attendance.www.hr.dao.mapper.AttachmentMapper;
import com.attendance.www.hr.dao.mapper.CorporationMapper;
import com.attendance.www.hr.dao.mapper.DepartmentMapper;
import com.attendance.www.hr.dao.mapper.EmployeeMapper;
import com.attendance.www.hr.dao.mapper.ReportAttendanceExtMapper;
import com.attendance.www.hr.dao.mapper.SalaryExtMapper;
import com.attendance.www.hr.dao.mapper.SalaryMapper;
import com.attendance.www.hr.dao.mapper.SalaryParamMapper;
import com.attendance.www.hr.dao.mapper.SalaryRequestMapper;
import com.attendance.www.hr.dao.mapper.SalaryTempExtMapper;
import com.attendance.www.hr.dao.mapper.SalaryTempMapper;
import com.attendance.www.hr.entitys.Attachment;
import com.attendance.www.hr.entitys.Corporation;
import com.attendance.www.hr.entitys.CorporationExample;
import com.attendance.www.hr.entitys.Department;
import com.attendance.www.hr.entitys.DepartmentExample;
import com.attendance.www.hr.entitys.Employee;
import com.attendance.www.hr.entitys.Salary;
import com.attendance.www.hr.entitys.SalaryExample;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IClosedService;
import com.attendance.www.hr.service.IReportResultService;
import com.attendance.www.hr.service.IReportService;
import com.attendance.www.hr.service.ISalaryService;

@Service
public class SalaryServiceImpl implements ISalaryService {
	
	@Resource
	private SalaryMapper salaryDao;
	@Resource
	private SalaryExtMapper salaryExtDao;
	@Resource
	private CorporationMapper corporationDao;
	@Resource
	private DepartmentMapper departmentDao;
	@Resource
	private EmployeeMapper employeeDao;
	@Resource
	private ReportAttendanceExtMapper reportDao;
	@Resource
	private IReportResultService  reportResultService;
	@Resource
	private IReportService reportService;
	@Resource
	private AttachmentMapper attachmentDao;
	@Resource
	private IClosedService closedService;
	@Resource
	private SalaryRequestMapper salaryRequestDao;
	@Resource
	private SalaryTempMapper salaryTempDao;
	@Resource
	private SalaryTempExtMapper salaryTempExtDao;
	@Resource
	private SalaryParamMapper salaryParamDao;

	/**
	 * 查询保存历史明细(右侧下拉框)
	 */
	@Override
	public List<SalaryBean> getSalaryHistory(Integer employeeId) {
		Employee emp = employeeDao.selectByPrimaryKey(employeeId);
		String employeeNoSubmit = emp.getEmployeeNo();
		return salaryExtDao.getSalaryHistory(employeeNoSubmit);
	}
	
//	private static Logger logger = Logger.getLogger(SalaryServiceImpl.class);
	/**
	 * 查询工资报表原数据
	 * 以开始日期，结束日期为条件（公司等可选）
	 * 或者点“历史”时，
	 */
	@Override
	public SalaryPageBean getSalaryTempList(SalaryBean salaryBean) {
//		Calendar calendar2= Calendar.getInstance();
//		SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//		logger.info("时间："+dateFormat.format(calendar2.getTime()));
		
		Employee emp = employeeDao.selectByPrimaryKey(salaryBean.getEmployeeId());
		String employeeNoSubmit = emp.getEmployeeNo();
		salaryBean.setEmployeeNoSubmit(employeeNoSubmit);
		
		List<SalaryBean> salaryBeanSelectList = new ArrayList<>();
		int count = 0;
		boolean hasHistoryData = false;
		
		//判断是新查询，还是看历史
		if (salaryBean.getHistoryStartDate()==null && salaryBean.getHistoryEndDate()==null) {//新查询，根据传的日期字段判断
			//页面的筛选条件
			Date startDate = salaryBean.getStartDate();
			Date endDate = salaryBean.getEndDate();
			//先查有没有保存的历史纪录
			SalaryExample checkExample = new SalaryExample();
			checkExample.createCriteria().andStartDateEqualTo(startDate)
			.andEndDateEqualTo(endDate)
			.andEmployeeNoSubmitEqualTo(employeeNoSubmit);
			List<Salary> checkList = salaryDao.selectByExample(checkExample);
			if (checkList.size()>0) {//有历史数据
				hasHistoryData = true;
			} else {
				//查一遍数据，报表计算结果的查询
				ReportAttendanceParam paramBean = new ReportAttendanceParam();
				paramBean.setBeginDate(startDate);
				paramBean.setEndDate(endDate);
				paramBean.setPageCount(salaryBean.getPageCount());
				paramBean.setStartNum(salaryBean.getStartNum());
				paramBean.setPageNo(salaryBean.getPageNo());
				if (salaryBean.getIsPage()!=null && salaryBean.getIsPage()==0) {
					paramBean.setIsPage(0);
				}
				//筛选查询
				//公司
				if (salaryBean.getCorporationList()==null || salaryBean.getCorporationList().size()==0) {
					paramBean.setCorporationList(new ArrayList<String>());
				} else {//传参了
					List<String> codeList = salaryBean.getCorporationList();
					paramBean.setCorporationList(codeList);
				}
				//部门
				if (salaryBean.getDepartmentList()==null || salaryBean.getDepartmentList().size()==0) {
					paramBean.setDepartmentList(new ArrayList<String>());
				} else {//传参了
					List<String> codeList = salaryBean.getDepartmentList();
					paramBean.setDepartmentList(codeList);
				}
				//人员
				if (salaryBean.getEmployeeList()==null || salaryBean.getEmployeeList().size()==0) {
					paramBean.setEmployeeList(new ArrayList<String>());
				} else {//传参了
					List<String> codeList = salaryBean.getEmployeeList();
					paramBean.setEmployeeList(codeList);
				}
				//人员级别判断
				if (salaryBean.getEmployeeLevelList()==null || salaryBean.getEmployeeLevelList().size()==0) {
					paramBean.setEmployeeLevelList(new ArrayList<Integer>());
				} else {//传参了
					List<Integer> levelList = salaryBean.getEmployeeLevelList();
					paramBean.setEmployeeLevelList(levelList);
				}
				
//				calendar2= Calendar.getInstance();
//				logger.info("执行：getReportByAuthority 开始时间："+dateFormat.format(calendar2.getTime()));
				//根据权限查看报表
				paramBean=reportService.getReportByAuthority(paramBean, salaryBean.getEmployeeId());
//				calendar2= Calendar.getInstance();
//				logger.info("执行：getReportByAuthority 结束时间："+dateFormat.format(calendar2.getTime()));
				
//				calendar2= Calendar.getInstance();
//				logger.info("执行：getReportResultInfoList 开始时间："+dateFormat.format(calendar2.getTime()));
				//报表最终的计算结果
				ReportAttendancePageBean rpb = reportResultService.getReportResultInfoList(paramBean);
//				calendar2= Calendar.getInstance();
//				logger.info("执行：getReportResultInfoList 结束时间："+dateFormat.format(calendar2.getTime()));
				
				List<ReportAttendanceBean> reportInfoList = rpb.getReportInfoList();
				
				//保存到临时数据
				List<SalaryBean> salaryBeanList = new ArrayList<>();
				for (ReportAttendanceBean reportAttendanceBean : reportInfoList) {
					SalaryBean salaryBeanTemp = new SalaryBean();
					
					salaryBeanTemp.setStartDate(startDate);
					salaryBeanTemp.setEndDate(endDate);
					salaryBeanTemp.setEmployeeNoSubmit(employeeNoSubmit);
					salaryBeanTemp.setAbsentCount(reportAttendanceBean.getAbsentCount());
					salaryBeanTemp.setBeAttendanceDays(reportAttendanceBean.getRealAttendanceDays());
					salaryBeanTemp.setChineseName(reportAttendanceBean.getChineseName());
					salaryBeanTemp.setDeptmentNameFive(reportAttendanceBean.getDeptmentNameFive());
					salaryBeanTemp.setDeptmentNameFour(reportAttendanceBean.getDeptmentNameFour());
					salaryBeanTemp.setDeptmentNameOne(reportAttendanceBean.getDeptmentNameOne());
					salaryBeanTemp.setDeptmentNameThree(reportAttendanceBean.getDeptmentNameThree());
					salaryBeanTemp.setDeptmentNameTwo(reportAttendanceBean.getDeptmentNameTwo());
					salaryBeanTemp.setEmployeeNo(reportAttendanceBean.getEmployeeNo());
					salaryBeanTemp.setEmployeeSeparatedDate(reportAttendanceBean.getEmployeeSeparatedDate());
					salaryBeanTemp.setEnglishName(reportAttendanceBean.getEnglishName());
					salaryBeanTemp.setEntryDate(reportAttendanceBean.getEntryDate());
					salaryBeanTemp.setLateCount(reportAttendanceBean.getLateCount());
//					salaryBeanTemp.setLateMoney(lateMoney);//计算规则 
					salaryBeanTemp.setLeaveEarlyCount(reportAttendanceBean.getLeaveEarlyCount());
//					salaryBeanTemp.setLeaveEarlyMoney(leaveEarlyMoney);//计算规则 
					salaryBeanTemp.setOvertimeHoliday(reportAttendanceBean.getOvertimeHoliday());
					salaryBeanTemp.setOvertimeNomal(reportAttendanceBean.getOvertimeNomal());
					salaryBeanTemp.setOvertimeWeekend(reportAttendanceBean.getOvertimeWeekend());
					salaryBeanTemp.setRestBornAttendUsed(reportAttendanceBean.getRestBornAttendUsed());
					salaryBeanTemp.setRestBornUsed(reportAttendanceBean.getRestBornUsed());
					salaryBeanTemp.setRestCheckUsed(reportAttendanceBean.getRestCheckUsed());
					salaryBeanTemp.setRestDieUsed(reportAttendanceBean.getRestDieUsed());
					salaryBeanTemp.setRestIllUsed(reportAttendanceBean.getRestIllUsed());
					salaryBeanTemp.setRestMarryUsed(reportAttendanceBean.getRestMarryUsed());
					salaryBeanTemp.setRestSuckleUsed(reportAttendanceBean.getRestSuckleUsed());
					salaryBeanTemp.setRestThingUsed(reportAttendanceBean.getRestThingUsed());
					salaryBeanTemp.setRestTuneUsed(reportAttendanceBean.getRestTuneUsed());
					salaryBeanTemp.setRestYearUsed(reportAttendanceBean.getRestYearUsed());
					salaryBeanTemp.setRetroactiveCount(reportAttendanceBean.getRetroactiveCount());
					salaryBeanTemp.setRoleName(reportAttendanceBean.getRoleName());
					salaryBeanTemp.setLevel(reportAttendanceBean.getLevel());
					salaryBeanTemp.setSex(reportAttendanceBean.getSex());
					salaryBeanList.add(salaryBeanTemp);
				}
				salaryBeanSelectList = salaryBeanList;
				count = rpb.getPageTotal();
			}
		}
		if ((salaryBean.getHistoryStartDate()!=null && salaryBean.getHistoryEndDate()!=null) || hasHistoryData) {//历史
			if (hasHistoryData) {
				salaryBean.setHistoryStartDate(salaryBean.getStartDate());
				salaryBean.setHistoryEndDate(salaryBean.getEndDate());
			}
			//筛选条件转换（code转name）查出公司name集合，部门name集合
			salaryBean = this.getSalaryBeanPropety(salaryBean);
			//从主数据表查
			salaryBeanSelectList = salaryExtDao.getSalaryList(salaryBean);
			count = salaryExtDao.count(salaryBean);
		}
		SalaryPageBean salaryPageBean = new SalaryPageBean();
		salaryPageBean.setPageTotal(count);
		salaryPageBean.setSalaryBeanList(salaryBeanSelectList);
		return salaryPageBean;
	}
	
	/**
	 * 筛选条件
	 * 查出公司name集合，部门name集合
	 * @param salaryBean
	 * @return
	 */
	private SalaryBean getSalaryBeanPropety(SalaryBean salaryBean) {
		//by公司筛选条件
		if (salaryBean.getCorporationList()!=null && salaryBean.getCorporationList().size()>0) {
			//查现有公司
			Map<String, String> cMap = new HashMap<>();//匹配公司code和公司名name
			CorporationExample corporationExample = new CorporationExample();
			corporationExample.or().andIsDeleteEqualTo(0)
			.andFromDateLessThanOrEqualTo(new Date())
			.andThruDateGreaterThanOrEqualTo(new Date());
			corporationExample.or().andIsDeleteEqualTo(0)
			.andFromDateLessThanOrEqualTo(new Date())
			.andThruDateIsNull();
			List<Corporation> cList = corporationDao.selectByExample(corporationExample);
			for (Corporation corporation : cList) {
				cMap.put(corporation.getCorporationCode(), corporation.getCorporationName());
			}
			//公司Name List
			List<String> corporationNameList = new ArrayList<>();
			//公司编号参数List
			List<String> corListParam = salaryBean.getCorporationList();
			for (String corNo : corListParam) {
				
				if (cMap.containsKey(corNo)) {
					corporationNameList.add(cMap.get(corNo));
				}
			}
			salaryBean.setCorporationNameList(corporationNameList);
		}
		//by部门筛选条件
		if (salaryBean.getDepartmentList()!=null && salaryBean.getDepartmentList().size()>0) {
			//查现有公司
			Map<String, String> dMap = new HashMap<>();//匹配部门code和部门名name
			DepartmentExample departmentExample = new DepartmentExample();
			departmentExample.or().andIsDisableEqualTo(0)
			.andFromDateLessThanOrEqualTo(new Date())
			.andThruDateGreaterThanOrEqualTo(new Date());
			departmentExample.or().andIsDisableEqualTo(0)
			.andFromDateLessThanOrEqualTo(new Date())
			.andThruDateIsNull();
			
			List<Department> depList = departmentDao.selectByExample(departmentExample);
			for (Department department : depList) {
				dMap.put(department.getDepartmentCode(), department.getDepartmentName());
			}
			
			//公司Name List
			List<String> departmentNameList = new ArrayList<>();
			//公司编号参数List
			List<String> depListParam = salaryBean.getDepartmentList();
			for (String depNo : depListParam) {
				if (dMap.containsKey(depNo)) {
					departmentNameList.add(dMap.get(depNo));
				}
			}
			salaryBean.setDepartmentNameList(departmentNameList);
		}
		return salaryBean;
	}
	
	/**
	 * 修改工资报表(对应“保存”按钮)
	 */
	@Override
	public boolean updateSalaryList(SalaryListBean salaryListBean) {
		int record = 0;
		
		//修改数据List
		List<SalaryBean> salaryBeanCorList = salaryListBean.getSalaryBeanList();

		Date startDate = salaryListBean.getStartDate();
		Date endDate = salaryListBean.getEndDate();
		Date historyStartDate = salaryListBean.getHistoryStartDate();
		Date historyEndDate = salaryListBean.getHistoryEndDate();
		if (startDate==null && endDate==null) {
			startDate = historyStartDate;
			endDate = historyEndDate;
		}
		Employee employeeSubmit = employeeDao.selectByPrimaryKey(salaryListBean.getEmployeeId());
		String employeeNoSubmit = employeeSubmit.getEmployeeNo();
		
		//查出此月历史数据，以开始时间和结束时间为条件
		SalaryExample salarySelExample = new SalaryExample();
		salarySelExample.createCriteria().andStartDateEqualTo(startDate)
		.andEndDateEqualTo(endDate)
		.andEmployeeNoSubmitEqualTo(employeeNoSubmit);
		List<Salary> salarySelList = salaryDao.selectByExample(salarySelExample);
		
		Map<String, Salary> employeeSalaryMap = new HashMap<>();
		for (Salary salary : salarySelList) {
			employeeSalaryMap.put(salary.getEmployeeNo(), salary);
		}
		
		//新查询，先保存计算值，再保存修改值
		if (salaryListBean.getHistoryStartDate()==null && salaryListBean.getHistoryEndDate()==null) {//新查询，根据传的日期字段判断
			//查报表计算结果数据
			ReportAttendanceParam paramBean = new ReportAttendanceParam();
			paramBean.setIsPage(0);//不分页时传 0
			paramBean.setBeginDate(startDate);
			paramBean.setEndDate(endDate);
			//筛选查询
			//公司
			if (salaryListBean.getCorporationList()==null || salaryListBean.getCorporationList().size()==0) {
				paramBean.setCorporationList(new ArrayList<String>());
			} else {//传参了
				List<String> codeList = salaryListBean.getCorporationList();
				paramBean.setCorporationList(codeList);
			}
			//部门
			if (salaryListBean.getDepartmentList()==null || salaryListBean.getDepartmentList().size()==0) {
				paramBean.setDepartmentList(new ArrayList<String>());
			} else {//传参了
				List<String> codeList = salaryListBean.getDepartmentList();
				paramBean.setDepartmentList(codeList);
			}
			//人员
			if (salaryListBean.getEmployeeList()==null || salaryListBean.getEmployeeList().size()==0) {
				paramBean.setEmployeeList(new ArrayList<String>());
			} else {//传参了
				List<String> codeList = salaryListBean.getEmployeeList();
				paramBean.setEmployeeList(codeList);
			}
			//人员级别判断
			if (salaryListBean.getEmployeeLevelList()==null || salaryListBean.getEmployeeLevelList().size()==0) {
				paramBean.setEmployeeLevelList(new ArrayList<Integer>());
			} else {//传参了
				List<Integer> levelList = salaryListBean.getEmployeeLevelList();
				paramBean.setEmployeeLevelList(levelList);
			}
			//根据权限查看报表
			paramBean=reportService.getReportByAuthority(paramBean, salaryListBean.getEmployeeId());
			//报表最终的计算结果
			ReportAttendancePageBean rpb = reportResultService.getReportResultInfoList(paramBean);
			List<ReportAttendanceBean> reportInfoList = rpb.getReportInfoList();
			//比较employeeId，有不操作，没有insert
			for (ReportAttendanceBean reportAttendanceBean : reportInfoList) {
				if (!employeeSalaryMap.containsKey(reportAttendanceBean.getEmployeeNo())) {//没有insert
					Salary salary = new Salary();
					
					salary.setStartDate(startDate);
					salary.setEndDate(endDate);
					salary.setEmployeeNoSubmit(employeeNoSubmit);
					salary = this.copyPropertiesFromReport(salary, reportAttendanceBean);
					salary.setRequestState(0);//初始为0 保存状态
					salary.setUpdateTime(new Date());
					salary.setCreateTime(new Date());
					record=salaryDao.insertSelective(salary);
					if (record<=0) {
						return false;
					}
					//没有的放入Map中
					employeeSalaryMap.put(salary.getEmployeeNo(), salary);
				}
			}
		}
		
		/*//查出此月历史数据
		List<Salary> salarySelListAfter = salaryDao.selectByExample(salarySelExample);
		for (Salary salary : salarySelListAfter) {
			employeeSalaryMap.put(salary.getEmployeeNo(), salary);
		}*/
		//更新修改值
		for (int i = 0; i < salaryBeanCorList.size(); i++) {
			SalaryBean salaryBn = salaryBeanCorList.get(i);
			Salary salary = employeeSalaryMap.get(salaryBn.getEmployeeNo());
			if (salary==null) {
				continue;
			}
			//判断审批状态
			Integer requestState = salary.getRequestState();
			
			if (requestState==1) {
				throw new ResultFaultException("16_00006");
			} else if (requestState==2) {
				throw new ResultFaultException("16_00007");
			}
			salary = this.copySalaryCor(salary, salaryBn);
			salary.setUpdateTime(new Date());
			salaryDao.updateByPrimaryKey(salary);
		}
		return true;
	}
	
	/**
	 * copy修改值
	 * @param salary
	 * @param salaryBean
	 * @return 主数据修改值
	 */
	private Salary copySalaryCor(Salary salary,SalaryBean salaryBean) {
//		salary.setChineseName(salaryBean.getChineseName());
		salary.setEmployeeNo(salaryBean.getEmployeeNo());
		salary.setAbsentCountCor(salaryBean.getAbsentCountCor());
		salary.setBeAttendanceDaysCor(salaryBean.getBeAttendanceDaysCor());
		salary.setLateCountCor(salaryBean.getLateCountCor());
		salary.setLateMoneyCor(salaryBean.getLateMoneyCor());
		salary.setLeaveEarlyCountCor(salaryBean.getLeaveEarlyCountCor());
		salary.setLeaveEarlyMoneyCor(salaryBean.getLeaveEarlyMoneyCor());
		salary.setOvertimeHolidayCor(salaryBean.getOvertimeHolidayCor());
		salary.setOvertimeNomalCor(salaryBean.getOvertimeNomalCor());
		salary.setOvertimeWeekendCor(salaryBean.getOvertimeWeekendCor());
		salary.setRestBornAttendUsedCor(salaryBean.getRestBornAttendUsedCor());
		salary.setRestBornUsedCor(salaryBean.getRestBornUsedCor());
		salary.setRestCheckUsedCor(salaryBean.getRestCheckUsedCor());
		salary.setRestDieUsedCor(salaryBean.getRestDieUsedCor());
		salary.setRestIllUsedCor(salaryBean.getRestIllUsedCor());
		salary.setRestMarryUsedCor(salaryBean.getRestMarryUsedCor());
		salary.setRestSuckleUsedCor(salaryBean.getRestSuckleUsedCor());
		salary.setRestThingUsedCor(salaryBean.getRestThingUsedCor());
		salary.setRestTuneUsedCor(salaryBean.getRestTuneUsedCor());
		salary.setRestYearUsedCor(salaryBean.getRestYearUsedCor());
		salary.setRetroactiveCountCor(salaryBean.getRetroactiveCountCor());
		salary.setDescription(salaryBean.getDescriptionCor());
		return salary;
	}
	
	/**
	 * copy属性 BeanUtils.copyProperties(salary, reportAttendanceBean);
	 * @param salary 
	 * @param reportAttendanceBean 报表计算数据
	 * @return
	 */
	private Salary copyPropertiesFromReport(Salary salary, ReportAttendanceBean reportAttendanceBean) {
		salary.setAbsentCount(reportAttendanceBean.getAbsentCount());
		salary.setBeAttendanceDays(reportAttendanceBean.getRealAttendanceDays());
		salary.setChineseName(reportAttendanceBean.getChineseName());
		salary.setDeptmentNameFive(reportAttendanceBean.getDeptmentNameFive());
		salary.setDeptmentNameFour(reportAttendanceBean.getDeptmentNameFour());
		salary.setDeptmentNameOne(reportAttendanceBean.getDeptmentNameOne());
		salary.setDeptmentNameThree(reportAttendanceBean.getDeptmentNameThree());
		salary.setDeptmentNameTwo(reportAttendanceBean.getDeptmentNameTwo());
		salary.setEmployeeNo(reportAttendanceBean.getEmployeeNo());
		salary.setEmployeeSeparatedDate(reportAttendanceBean.getEmployeeSeparatedDate());
		salary.setEnglishName(reportAttendanceBean.getEnglishName());
		salary.setEntryDate(reportAttendanceBean.getEntryDate());
		salary.setLateCount(reportAttendanceBean.getLateCount());
//		salary.setLateMoney(lateMoney);//计算规则 
		salary.setLeaveEarlyCount(reportAttendanceBean.getLeaveEarlyCount());
//		salary.setLeaveEarlyMoney(leaveEarlyMoney);//计算规则 
		salary.setOvertimeHoliday(reportAttendanceBean.getOvertimeHoliday());
		salary.setOvertimeNomal(reportAttendanceBean.getOvertimeNomal());
		salary.setOvertimeWeekend(reportAttendanceBean.getOvertimeWeekend());
		salary.setRestBornAttendUsed(reportAttendanceBean.getRestBornAttendUsed());
		salary.setRestBornUsed(reportAttendanceBean.getRestBornUsed());
		salary.setRestCheckUsed(reportAttendanceBean.getRestCheckUsed());
		salary.setRestDieUsed(reportAttendanceBean.getRestDieUsed());
		salary.setRestIllUsed(reportAttendanceBean.getRestIllUsed());
		salary.setRestMarryUsed(reportAttendanceBean.getRestMarryUsed());
		salary.setRestSuckleUsed(reportAttendanceBean.getRestSuckleUsed());
		salary.setRestThingUsed(reportAttendanceBean.getRestThingUsed());
		salary.setRestTuneUsed(reportAttendanceBean.getRestTuneUsed());
		salary.setRestYearUsed(reportAttendanceBean.getRestYearUsed());
		salary.setRetroactiveCount(reportAttendanceBean.getRetroactiveCount());
		salary.setRoleName(reportAttendanceBean.getRoleName());
		salary.setLevel(reportAttendanceBean.getLevel());
		salary.setSex(reportAttendanceBean.getSex());
		return salary;
	}

	/**
	 * 新增数据通过Excel，
	 * 增加没有生成主数据的信息，by人员编号
	 * @param employeeNoLackList 缺少的人员编号List
	 * @param startDate 开始日期
	 * @param endDate
	 * @param emp 人员entiy
	 * @param salaryYear 报表年份
	 * @param salaryMonth 报表月份
	 */
	private List<Salary> saveLackSalaryListByExcel(List<String> employeeNoLackList, Date startDate, Date endDate,Employee emp) {
		List<Salary> salaryList = new ArrayList<>();
		//查一遍数据
		//报表计算结果的查询
		ReportAttendanceParam paramBean = new ReportAttendanceParam();
		paramBean.setIsPage(0);//不分页时传 0
		paramBean.setBeginDate(startDate);
		paramBean.setEndDate(endDate);
		
		//公司
		paramBean.setCorporationList(new ArrayList<String>());
		//部门
		paramBean.setDepartmentList(new ArrayList<String>());
		//人员级别判断
		paramBean.setEmployeeLevelList(new ArrayList<Integer>());
		//要查的人员编号List
		paramBean.setEmployeeList(employeeNoLackList);
		
		//根据权限查看报表
		paramBean=reportService.getReportByAuthority(paramBean, emp.getEmployeeId());
		//报表最终的计算结果
		ReportAttendancePageBean rpb = reportResultService.getReportResultInfoList(paramBean);
		List<ReportAttendanceBean> reportInfoList = rpb.getReportInfoList();
		//插入主表
		for (ReportAttendanceBean reportAttendanceBean : reportInfoList) {
			Salary salary = new Salary();
			
			salary.setStartDate(startDate);
			salary.setEndDate(endDate);
			salary.setRequestState(0);//申请状态0
			
			salary.setEmployeeNoSubmit(emp.getEmployeeNo());
			salary.setAbsentCount(reportAttendanceBean.getAbsentCount());
			salary.setBeAttendanceDays(reportAttendanceBean.getRealAttendanceDays());
			salary.setChineseName(reportAttendanceBean.getChineseName());
			salary.setDeptmentNameFive(reportAttendanceBean.getDeptmentNameFive());
			salary.setDeptmentNameFour(reportAttendanceBean.getDeptmentNameFour());
			salary.setDeptmentNameOne(reportAttendanceBean.getDeptmentNameOne());
			salary.setDeptmentNameThree(reportAttendanceBean.getDeptmentNameThree());
			salary.setDeptmentNameTwo(reportAttendanceBean.getDeptmentNameTwo());
			salary.setEmployeeNo(reportAttendanceBean.getEmployeeNo());
			salary.setEmployeeSeparatedDate(reportAttendanceBean.getEmployeeSeparatedDate());
			salary.setEnglishName(reportAttendanceBean.getEnglishName());
			salary.setEntryDate(reportAttendanceBean.getEntryDate());
			salary.setLateCount(reportAttendanceBean.getLateCount());
//			salary.setLateMoney(lateMoney);//计算规则 
			salary.setLeaveEarlyCount(reportAttendanceBean.getLeaveEarlyCount());
//			salary.setLeaveEarlyMoney(leaveEarlyMoney);//计算规则 
			salary.setOvertimeHoliday(reportAttendanceBean.getOvertimeHoliday());
			salary.setOvertimeNomal(reportAttendanceBean.getOvertimeNomal());
			salary.setOvertimeWeekend(reportAttendanceBean.getOvertimeWeekend());
			salary.setRestBornAttendUsed(reportAttendanceBean.getRestBornAttendUsed());
			salary.setRestBornUsed(reportAttendanceBean.getRestBornUsed());
			salary.setRestCheckUsed(reportAttendanceBean.getRestCheckUsed());
			salary.setRestDieUsed(reportAttendanceBean.getRestDieUsed());
			salary.setRestIllUsed(reportAttendanceBean.getRestIllUsed());
			salary.setRestMarryUsed(reportAttendanceBean.getRestMarryUsed());
			salary.setRestSuckleUsed(reportAttendanceBean.getRestSuckleUsed());
			salary.setRestThingUsed(reportAttendanceBean.getRestThingUsed());
			salary.setRestTuneUsed(reportAttendanceBean.getRestTuneUsed());
			salary.setRestYearUsed(reportAttendanceBean.getRestYearUsed());
			salary.setRetroactiveCount(reportAttendanceBean.getRetroactiveCount());
			salary.setRoleName(reportAttendanceBean.getRoleName());
			salary.setLevel(reportAttendanceBean.getLevel());
			salary.setSex(reportAttendanceBean.getSex());
			salaryDao.insertSelective(salary);
			salaryList.add(salary);
		}
		return salaryList;
	}
	
	
	/**
	 * 导入工资报表
	 */
	@Override
	public List<String> saveSalaryListExcel(Integer attachmentId, Integer employeeId) {
		List<String> successList = new ArrayList<String>();
		// 查询附件表获取路径地址
		Attachment attachment = attachmentDao.selectByPrimaryKey(attachmentId);
		// 设置文件路径
		String pathName = attachment.getUrl().replace(AppConstant.UPLOAD_RESOURCE, AppConstant.SAVE_FILE_REPOSITORY);
		File file = new File(pathName);
		Workbook work = null;
		if (file.exists()) {
			try {
				InputStream inputStream = new FileInputStream(file);
				if (pathName.matches("^.+\\.(?i)(xls)$")) {
					work = new HSSFWorkbook(inputStream);
				} else if (pathName.matches("^.+\\.(?i)(xlsx)$")) {
					work = new XSSFWorkbook(inputStream);
				}
				if (inputStream != null) {
					inputStream.close();
				}
			} catch (Exception ex) {
				throw new ResultFaultException("0_00003");
			}
		} else {
			throw new ResultFaultException("0_00003");
		}
		if (work == null) {
			throw new ResultFaultException("0_00003");
		}
		if (work != null) {
			successList = addSalaryExcel(work,successList,employeeId);
		}
		return successList;
	}
	
	/**
	 * 导入excel表格，逐行读取操作
	 * @param work
	 * @param successList
	 * @return
	 */
	private List<String> addSalaryExcel(Workbook work,List<String> successList,Integer employeeId) {
		//提交工资报表人
		Employee emp = employeeDao.selectByPrimaryKey(employeeId);
		//获取工作表的数量
		int sheetNum = work.getNumberOfSheets();
		//遍历工作表 找出要导入的Excel表
		for (int m = 0; m < sheetNum; m++) {
			Sheet sh = work.getSheetAt(m);
			if (sh.getSheetName().equals("考勤报表（工资使用）") || sh.getSheetName().equals("报表记录明细")) {
				boolean isDetails = sh.getSheetName().equals("报表记录明细");
				Sheet sheet = work.getSheetAt(m);
				List<String> errorMsgList = new ArrayList<String>();// 创建错误信息列表
				
				Map<String, Salary> salaryMap = new HashMap<>();//key--employeeNo员工编号
				
				//获取报表的开始结束日期
				Date startDate = new Date();
				Date endDate = new Date();
				
				Row rowDate = sheet.getRow(0);

				SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
				// 判断开始时间
				if (rowDate.getCell(1) != null && StringUtils.isNotBlank(rowDate.getCell(1).toString())) {
					try {//如果是日期格式
						startDate = rowDate.getCell(1).getDateCellValue();
					} catch (Exception e) {
						String startDateStr = rowDate.getCell(1).getStringCellValue();
						try {//如果是字符串格式
							startDate = sdf.parse(startDateStr);
						} catch (ParseException e1) {
							e1.printStackTrace();
							errorMsgList.add("第1行，开始时间格式不正确。");
						}
					}
					if (startDate != null) {
					} else {
						errorMsgList.add("第1行，开始时间格式不正确。");
					}
				} else {
					errorMsgList.add("第1行，开始时间不能为空。");
				}
				//结束时间
				if (rowDate.getCell(3) != null && StringUtils.isNotBlank(rowDate.getCell(3).toString())) {
					try {//如果是日期格式
						endDate = rowDate.getCell(3).getDateCellValue();
					} catch (Exception e) {
						String endDateStr = rowDate.getCell(3).getStringCellValue();
						try {//如果是字符串格式
							endDate = sdf.parse(endDateStr);
						} catch (ParseException e1) {
							e1.printStackTrace();
							errorMsgList.add("第1行，结束时间格式不正确。");
						}
					}
					if (endDate != null) {
					} else {
						errorMsgList.add("第1行，结束时间格式不正确。");
					}
				} else {
					errorMsgList.add("第1行，结束时间不能为空。");
				}
				if (errorMsgList.size() > 0) {
					throw new ResultFaultException("8_00002", errorMsgList);
				}
				
				/*Calendar calendar = Calendar.getInstance();
		        calendar.setTime(endDate);
		        
		        int year = calendar.get(Calendar.YEAR);
		        int month = calendar.get(Calendar.MONTH);
				//封账验证 用结束时间的年月
				if (closedService.verifyClosed(year, month)) {
					throw new ResultFaultException("4_00003");
				}*/
		        
				SalaryExample salaryExample = new SalaryExample();
				salaryExample.createCriteria().andStartDateEqualTo(startDate)
				.andEndDateEqualTo(endDate)
				.andEmployeeNoSubmitEqualTo(emp.getEmployeeNo());
				List<Salary> salaryList = salaryDao.selectByExample(salaryExample);
				for (Salary salary : salaryList) {
					salaryMap.put(salary.getEmployeeNo(), salary);
				}
				String employeeNo = null;
				List<SalaryBean> salaryBeanList = new ArrayList<>();
				
				List<String> employeeNoLackList = new ArrayList<>();//没保存的员工编号List
				// 扫描Excel每行的每个单元格
				for (int i = 1; i <= sheet.getLastRowNum(); i++) {
					StringBuilder str = new StringBuilder("第" + (i + 3) + "行");
					//从第4行开始取数据
					Row row = sheet.getRow(i+2);
					// 如果一整行为空则结束循环
					if (row == null) {
						//如果有缺少数据，先新增
						if (employeeNoLackList.size()>0) {
							List<Salary> lackList = this.saveLackSalaryListByExcel(employeeNoLackList, startDate, endDate, emp);
							salaryList.addAll(lackList);
						}
						
						if (salaryBeanList.size()>0) {
							//进行修改值比较的map
							Map<String, Salary> salaryEqualMap = new HashMap<>();
							for (Salary salary : salaryList) {
								salaryEqualMap.put(salary.getEmployeeNo(), salary);
							}
							
							//Excel中的值与数据库中的值比较
							for (int j = 0; j < salaryBeanList.size(); j++) {
								
//								SalaryBean salaryBean = salaryBeanList.get(j);
								Salary salary = salaryEqualMap.get(salaryBeanList.get(j).getEmployeeNo());
								//对导入null值变0的处理
								List<String> tempList = new ArrayList<>();
								this.compareValue(salaryBeanList.get(j), salary, tempList, j);
								if (!StringUtils.isBlank(salaryBeanList.get(j).getDescriptionCor())) {//备注说明
									continue;
								}
								//比较修改值，返回错误信息List
								errorMsgList = this.compareValue(salaryBeanList.get(j), salary, errorMsgList, j);
							}
							if (errorMsgList.size() > 0) {
					            errorMsgList.add("未填写更改备注，请填写后导入。");
								throw new ResultFaultException("8_00002", errorMsgList);
							}
							SalaryListBean salaryListBean = new SalaryListBean();
							salaryListBean.setSalaryBeanList(salaryBeanList);
							salaryListBean.setEmployeeId(employeeId);
							salaryListBean.setHistoryStartDate(startDate);
							salaryListBean.setHistoryEndDate(endDate);
							//修改cor
							boolean isRecord = this.updateSalaryList(salaryListBean);
							if (isRecord==false) {
								throw new ResultFaultException("0_00008");//修改失败
							}
						}
						break;
					}
					//遍历必填项列，返回错误信息
					boolean isAllNull = true;//如果所有行都为Null，就break
					for (int c = 0; c < 30; c++) {
						if (row.getCell(c) == null || StringUtils.isBlank(row.getCell(c).toString())) {
							
						} else {
							isAllNull = false;
							break;
						}
					}
					if (isAllNull) {//此行都为空值，break结束循环
						//如果有缺少数据，先新增
						if (employeeNoLackList.size()>0) {
							this.saveLackSalaryListByExcel(employeeNoLackList, startDate, endDate, emp);
						}
						
						if (salaryBeanList.size()>0) {
							SalaryListBean salaryListBean = new SalaryListBean();
							salaryListBean.setSalaryBeanList(salaryBeanList);
							salaryListBean.setEmployeeId(employeeId);
							salaryListBean.setHistoryStartDate(startDate);
							salaryListBean.setHistoryEndDate(endDate);
							//修改cor
							boolean isRecord = this.updateSalaryList(salaryListBean);
							if (isRecord==false) {
								throw new ResultFaultException("0_00008");//修改失败
							}
						}
						break;
					}
					// 判断是否为数字
					if (row.getCell(5).getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
						employeeNo = new BigDecimal(row.getCell(5).getNumericCellValue()).intValue() + "";
					} else {
						employeeNo = row.getCell(5).toString();
					}
					
					//比较没保存的员工编号，放入List
					if (!salaryMap.containsKey(employeeNo)) {
						employeeNoLackList.add(employeeNo);
					}
					//保存修改值
					SalaryBean salaryBean = new SalaryBean();
					//保存必要参数
					salaryBean.setIsTemp(1);
					salaryBean.setEmployeeNoSubmit(emp.getEmployeeNo());
					salaryBean.setStartDate(startDate);
					salaryBean.setEndDate(endDate);
					//公司名称
					if (row.getCell(0) != null && !StringUtils.isBlank(row.getCell(0).toString())) {
						salaryBean.setDeptmentNameOne(row.getCell(0).getStringCellValue());
					} else {
						str.append("第"+1+"列的值不能为空,");
					}
					//员工编号employeeNo
					if (row.getCell(5) != null && !StringUtils.isBlank(row.getCell(5).toString())) {
						salaryBean.setEmployeeNo(row.getCell(5).getStringCellValue());
					} else {
						str.append("第"+5+"列的值不能为空,");
					}
					
					//判断是否有做修改
					boolean isUpdate = false;
					int cols = 12;//第几列
					//应出勤天数 修改值
					//cols++;
					if (row.getCell(cols) != null && !StringUtils.isBlank(row.getCell(cols).toString())) {
						if (row.getCell(cols).getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
							salaryBean.setBeAttendanceDaysCor(new Double(row.getCell(cols).getNumericCellValue()).intValue());
							isUpdate = true;
						} else {
							str.append("第"+(cols+1)+"列的值不合法,");
						}
					}
					//迟到次数 修改值
					cols++;
					if (row.getCell(cols) != null && !StringUtils.isBlank(row.getCell(cols).toString())) {
						if (row.getCell(cols).getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
							salaryBean.setLateCountCor(new Double(row.getCell(cols).getNumericCellValue()).intValue());
							isUpdate = true;
						} else {
							str.append("第"+(cols+1)+"列的值不合法,");
						}
					}
					//迟到扣款 修改值
					cols++;
					if (row.getCell(cols) != null && !StringUtils.isBlank(row.getCell(cols).toString())) {
						if (row.getCell(cols).getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
							salaryBean.setLateMoneyCor(new Double(row.getCell(cols).getNumericCellValue()).intValue());
							isUpdate = true;
						} else {
							str.append("第"+(cols+1)+"列的值不合法,");
						}
					}
					//早退次数 修改值
					cols++;
					if (row.getCell(cols) != null && !StringUtils.isBlank(row.getCell(cols).toString())) {
						if (row.getCell(cols).getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
							salaryBean.setLeaveEarlyCountCor(new Double(row.getCell(cols).getNumericCellValue()).intValue());
							isUpdate = true;
						} else {
							str.append("第"+(cols+1)+"列的值不合法,");
						}
					}
					//早退扣款 修改值
					cols++;
					if (row.getCell(cols) != null && !StringUtils.isBlank(row.getCell(cols).toString())) {
						if (row.getCell(cols).getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
							salaryBean.setLeaveEarlyMoneyCor(new Double(row.getCell(cols).getNumericCellValue()).intValue());
							isUpdate = true;
						} else {
							str.append("第"+(cols+1)+"列的值不合法,");
						}
					}
					//旷工次数 修改值
					cols++;
					if (row.getCell(cols) != null && !StringUtils.isBlank(row.getCell(cols).toString())) {
						if (row.getCell(cols).getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
							salaryBean.setAbsentCountCor(new Double(row.getCell(cols).getNumericCellValue()).intValue());
							isUpdate = true;
						} else {
							str.append("第"+(cols+1)+"列的值不合法,");
						}
					}
					//补签次数 修改值
					cols++;
					if (row.getCell(cols) != null && !StringUtils.isBlank(row.getCell(cols).toString())) {
						if (row.getCell(cols).getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
							salaryBean.setRetroactiveCountCor(new Double(row.getCell(cols).getNumericCellValue()).intValue());
							isUpdate = true;
						} else {
							str.append("第"+(cols+1)+"列的值不合法,");
						}
					}
					//平日加班 修改值
					cols++;
					if (row.getCell(cols) != null && !StringUtils.isBlank(row.getCell(cols).toString())) {
						if (row.getCell(cols).getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
							salaryBean.setOvertimeNomalCor(new Double(row.getCell(cols).getNumericCellValue()).floatValue());
							isUpdate = true;
						} else {
							str.append("第"+(cols+1)+"列的值不合法,");
						}
					}
					//双休加班 修改值
					cols++;
					if (row.getCell(cols) != null && !StringUtils.isBlank(row.getCell(cols).toString())) {
						if (row.getCell(cols).getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
							salaryBean.setOvertimeWeekendCor(new Double(row.getCell(cols).getNumericCellValue()).floatValue());
							isUpdate = true;
						} else {
							str.append("第"+(cols+1)+"列的值不合法,");
						}
					}
					//节日加班 修改值
					cols++;
					if (row.getCell(cols) != null && !StringUtils.isBlank(row.getCell(cols).toString())) {
						if (row.getCell(cols).getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
							salaryBean.setOvertimeHolidayCor(new Double(row.getCell(cols).getNumericCellValue()).floatValue());
							isUpdate = true;
						} else {
							str.append("第"+(cols+1)+"列的值不合法,");
						}
					}
					//年假 修改值
					cols++;
					if (row.getCell(cols) != null && !StringUtils.isBlank(row.getCell(cols).toString())) {
						if (row.getCell(cols).getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
							salaryBean.setRestYearUsedCor(new Double(row.getCell(cols).getNumericCellValue()).floatValue());
							isUpdate = true;
						} else {
							str.append("第"+(cols+1)+"列的值不合法,");
						}
					}
					//调休 修改值
					cols++;
					if (row.getCell(cols) != null && !StringUtils.isBlank(row.getCell(cols).toString())) {
						if (row.getCell(cols).getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
							salaryBean.setRestTuneUsedCor(new Double(row.getCell(cols).getNumericCellValue()).floatValue());
							isUpdate = true;
						} else {
							str.append("第"+(cols+1)+"列的值不合法,");
						}
					}
					//产假 修改值
					cols++;
					if (row.getCell(cols) != null && !StringUtils.isBlank(row.getCell(cols).toString())) {
						if (row.getCell(cols).getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
							salaryBean.setRestBornUsedCor(new Double(row.getCell(cols).getNumericCellValue()).intValue());
							isUpdate = true;
						} else {
							str.append("第"+(cols+1)+"列的值不合法,");
						}
					}
					//产检 修改值
					cols++;
					if (row.getCell(cols) != null && !StringUtils.isBlank(row.getCell(cols).toString())) {
						if (row.getCell(cols).getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
							salaryBean.setRestCheckUsedCor(new Double(row.getCell(cols).getNumericCellValue()).floatValue());
							isUpdate = true;
						} else {
							str.append("第"+(cols+1)+"列的值不合法,");
						}
					}
					//丧假 修改值
					cols++;
					if (row.getCell(cols) != null && !StringUtils.isBlank(row.getCell(cols).toString())) {
						if (row.getCell(cols).getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
							salaryBean.setRestDieUsedCor(new Double(row.getCell(cols).getNumericCellValue()).intValue());
							isUpdate = true;
						} else {
							str.append("第"+(cols+1)+"列的值不合法,");
						}
					}
					//婚假 修改值
					cols++;
					if (row.getCell(cols) != null && !StringUtils.isBlank(row.getCell(cols).toString())) {
						if (row.getCell(cols).getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
							salaryBean.setRestMarryUsedCor(new Double(row.getCell(cols).getNumericCellValue()).intValue());
							isUpdate = true;
						} else {
							str.append("第"+(cols+1)+"列的值不合法,");
						}
					}
					//陪护假 修改值
					cols++;
					if (row.getCell(cols) != null && !StringUtils.isBlank(row.getCell(cols).toString())) {
						if (row.getCell(cols).getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
							salaryBean.setRestBornAttendUsedCor(new Double(row.getCell(cols).getNumericCellValue()).intValue());
							isUpdate = true;
						} else {
							str.append("第"+(cols+1)+"列的值不合法,");
						}
					}
					//哺乳假 修改值
					cols++;
					if (row.getCell(cols) != null && !StringUtils.isBlank(row.getCell(cols).toString())) {
						if (row.getCell(cols).getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
							salaryBean.setRestSuckleUsedCor(new Double(row.getCell(cols).getNumericCellValue()).floatValue());
							isUpdate = true;
						} else {
							str.append("第"+(cols+1)+"列的值不合法,");
						}
					}
					//病假 修改值
					cols++;
					if (row.getCell(cols) != null && !StringUtils.isBlank(row.getCell(cols).toString())) {
						if (row.getCell(cols).getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
							salaryBean.setRestIllUsedCor(new Double(row.getCell(cols).getNumericCellValue()).floatValue());
							isUpdate = true;
						} else {
							str.append("第"+(cols+1)+"列的值不合法,");
						}
					}
					//事假 修改值
					cols++;
					if (row.getCell(cols) != null && !StringUtils.isBlank(row.getCell(cols).toString())) {
						if (row.getCell(cols).getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
							salaryBean.setRestThingUsedCor(new Double(row.getCell(cols).getNumericCellValue()).floatValue());
							isUpdate = true;
						} else {
							str.append("第"+(cols+1)+"列的值不合法,");
						}
					}
					//备注
					cols++;
					if (isDetails) {//sh.getSheetName().equals("报表记录明细")
						cols=53;//是报表明细，取54列的备注
					}
					if (row.getCell(cols) != null && !StringUtils.isBlank(row.getCell(cols).toString())) {
						try {//字符串格式
							salaryBean.setDescriptionCor(row.getCell(cols).getStringCellValue());
						} catch (Exception e) {//数字格式
							Double descDouble = row.getCell(cols).getNumericCellValue();
							salaryBean.setDescriptionCor(descDouble.toString());
						}
					}
					if (isUpdate) {
						salaryBeanList.add(salaryBean);
					}
					// 判断错误信息长度
					if (str.length() == new String("第" + (i + 3) + "行").length()) {
						
					} else {
						String errorStr = str.toString().substring(0, str.toString().length() - 1);
						errorMsgList.add(errorStr);
					}
				}
				if (errorMsgList.size() > 0) {
					throw new ResultFaultException("8_00002", errorMsgList);
				}
				successList.add(0, "考勤信息导入完成。");
				return successList;
			}
		}
		throw new ResultFaultException("0_00014");
	}

	/**
	 * 导出工资报表
	 */
	@Override
	public String getExportSalary(SalaryBean salaryBeanParam) {
		salaryBeanParam.setIsPage(0);//不分页，查全部数据
		
			long time = System.currentTimeMillis();
			String readUrl = AppConstant.TEMPLATEFILEREPOSITORY + "/考勤报表（工资使用）.xlsx";
			String fileurl = AppConstant.REPORTFILEREPOSITORY + "/考勤报表（工资使用）" + time + ".xlsx";
			String url = AppConstant.REPORT_RESOURCE + "/考勤报表（工资使用）" + time + ".xlsx";
			File file = new File(readUrl);
			if (!file.exists()) {
				try {
					file.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			InputStream inputStream =null;
			Workbook wb = null;
			try {
				inputStream = new FileInputStream(readUrl);
				if (readUrl.matches("^.+\\.(?i)(xls)$")) {
					wb = new HSSFWorkbook(inputStream);
				} else if (readUrl.matches("^.+\\.(?i)(xlsx)$")) {
					wb = new XSSFWorkbook(inputStream);
				} else {
					if (inputStream!=null) {
						inputStream.close();
					}
					throw new ResultFaultException("8_00001");
				}
			} catch (Exception e) {
				return null;
			}
			Sheet sheet = wb.getSheetAt(0);

			// 创建单元格样式对象
			XSSFCellStyle ztStyle = (XSSFCellStyle) wb.createCellStyle();
			//日期格式
			XSSFCellStyle dateZtStyle = (XSSFCellStyle) wb.createCellStyle();
			
			// 设置字体样式
			Font ztFont = wb.createFont();
			ztFont.setFontHeightInPoints((short) 9); // 将字体大小设置为18px
			ztFont.setFontName("微软雅黑"); // 将“华文行楷”字体应用到当前单元格上
			ztFont.setColor(IndexedColors.BLACK.index);
			
			// 设置字体样式
			Font redZtFont = wb.createFont();
			redZtFont.setFontHeightInPoints((short) 9); // 将字体大小设置为18px
			redZtFont.setFontName("微软雅黑"); // 将“华文行楷”字体应用到当前单元格上
			redZtFont.setColor(IndexedColors.RED.index);
			ztStyle.setFont(ztFont);
			
			// 设置边框颜色
			ztStyle.setBorderBottom(CellStyle.BORDER_THIN);
			ztStyle.setBorderTop(CellStyle.BORDER_THIN);
			ztStyle.setBorderLeft(CellStyle.BORDER_THIN);
			ztStyle.setBorderRight(CellStyle.BORDER_THIN);
			
			dateZtStyle.setFont(ztFont);
			// 设置边框颜色
			dateZtStyle.setBorderBottom(CellStyle.BORDER_THIN);
			dateZtStyle.setBorderTop(CellStyle.BORDER_THIN);
			dateZtStyle.setBorderLeft(CellStyle.BORDER_THIN);
			dateZtStyle.setBorderRight(CellStyle.BORDER_THIN);

			// 设置单元格边框颜色
			XSSFColor ztColor = new XSSFColor(java.awt.Color.black);
			ztStyle.setBottomBorderColor(ztColor);
			ztStyle.setTopBorderColor(ztColor);
			ztStyle.setLeftBorderColor(ztColor);
			ztStyle.setRightBorderColor(ztColor);
			
			dateZtStyle.setBottomBorderColor(ztColor);
			dateZtStyle.setTopBorderColor(ztColor);
			dateZtStyle.setLeftBorderColor(ztColor);
			dateZtStyle.setRightBorderColor(ztColor);

			DataFormat fmt = wb.createDataFormat();  
			dateZtStyle.setDataFormat(fmt.getFormat("yyyy-MM-dd"));
			
			//查数据
			List<SalaryBean> resultList = new ArrayList<>();
			if (salaryBeanParam.getSalaryRequestId()!=null) {
				//审批人根据申请ID下载报表
				resultList = salaryExtDao.getSalaryListById(salaryBeanParam.getSalaryRequestId());
			} else if (salaryBeanParam.getSalaryYear()!=null && salaryBeanParam.getSalaryMonth()!=null) {
				//下载工资汇总表
				resultList = salaryExtDao.getSalaryTotalList(salaryBeanParam);
			} else {
				//本人下载
				resultList = this.getSalaryTempList(salaryBeanParam).getSalaryBeanList();
			}
			if (resultList.size()==0) {
				throw new ResultFaultException("16_00002");
			}
			//写表头
			Row rowTitle = sheet.createRow(0);
			Cell cellTitle = rowTitle.createCell(4);
            cellTitle.setCellStyle(ztStyle);
            cellTitle.setCellValue("考勤表");
            cellTitle = rowTitle.createCell(0);
            cellTitle.setCellStyle(ztStyle);
            cellTitle.setCellValue("开始日期");
            cellTitle = rowTitle.createCell(2);
            cellTitle.setCellStyle(ztStyle);
            cellTitle.setCellValue("结束日期");

			cellTitle = rowTitle.createCell(1);
			cellTitle.setCellStyle(dateZtStyle);
			Date startDate=resultList.get(0).getStartDate();
			cellTitle.setCellValue(startDate);
			
			cellTitle = rowTitle.createCell(3);
			cellTitle.setCellStyle(dateZtStyle);
			Date endDate = resultList.get(0).getEndDate();
			cellTitle.setCellValue(endDate);
			
			for (int i = 0; i < resultList.size(); i++) {
				SalaryBean salaryBean = resultList.get(i);
				if (salaryBean == null) {
					continue;
				}
				Row row = sheet.createRow(i + 3);
				Cell cell = row.createCell(0);
				cell.setCellStyle(ztStyle);
				if (salaryBean.getDeptmentNameOne() != null) {
					cell.setCellValue(salaryBean.getDeptmentNameOne());
				} else {
					cell.setCellValue("");
				}
				cell = row.createCell(1);
				cell.setCellStyle(ztStyle);
				if (salaryBean.getDeptmentNameTwo() != null) {
					cell.setCellValue(salaryBean.getDeptmentNameTwo());
				} else {
					cell.setCellValue("");
				}
				cell = row.createCell(2);
				cell.setCellStyle(ztStyle);
				if (salaryBean.getDeptmentNameThree() != null) {
					cell.setCellValue(salaryBean.getDeptmentNameThree());
				} else {
					cell.setCellValue("");
				}
				cell = row.createCell(3);
				cell.setCellStyle(ztStyle);
				if (salaryBean.getDeptmentNameFour() != null) {
					cell.setCellValue(salaryBean.getDeptmentNameFour());
				} else {
					cell.setCellValue("");
				}
				cell = row.createCell(4);
				cell.setCellStyle(ztStyle);
				if (salaryBean.getDeptmentNameFive() != null) {
					cell.setCellValue(salaryBean.getDeptmentNameFive());
				} else {
					cell.setCellValue("");
				}
				cell = row.createCell(5);
				cell.setCellStyle(ztStyle);
				if (salaryBean.getEmployeeNo() != null) {
					cell.setCellValue(salaryBean.getEmployeeNo());
				} else {
					cell.setCellValue("");
				}
				cell = row.createCell(6);
				cell.setCellStyle(ztStyle);
				if (salaryBean.getChineseName() != null) {
					cell.setCellValue(salaryBean.getChineseName());
				} else {
					cell.setCellValue("");
				}
				
				cell = row.createCell(7);
				cell.setCellStyle(ztStyle);
				if (salaryBean.getSex() != null) {
					if(salaryBean.getSex()==1) {
						cell.setCellValue("男");
					}else{
						cell.setCellValue("女");
					}
				} else {
					cell.setCellValue("");
				}
				
				cell = row.createCell(8);
				cell.setCellStyle(ztStyle);
				if (salaryBean.getLevel() != null) {
					cell.setCellValue(salaryBean.getLevel());
				} else {
					cell.setCellValue("");
				}
				
				cell = row.createCell(9);
				cell.setCellStyle(ztStyle);
				if (salaryBean.getRoleName() != null) {
					cell.setCellValue(salaryBean.getRoleName());
				} else {
					cell.setCellValue("");
				}
				
				cell = row.createCell(10);
				cell.setCellStyle(dateZtStyle);
				if (salaryBean.getEntryDate() != null) {
					Date date=salaryBean.getEntryDate();  
					//String str=sdf.format(date);  
					cell.setCellValue(date);
				} else {
					cell.setCellValue("");
				}
				cell = row.createCell(11);
				cell.setCellStyle(dateZtStyle);
				if (salaryBean.getEmployeeSeparatedDate() != null) {
					Date date=salaryBean.getEmployeeSeparatedDate();  
					//String str=sdf.format(date);  
					cell.setCellValue(date);
				} else {
					cell.setCellValue("");
				}
				//应出勤天数
				cell = row.createCell(12);
				cell.setCellStyle(ztStyle);
				if (salaryBean.getBeAttendanceDaysCor() != null || salaryBean.getBeAttendanceDays() != null) {
					if(salaryBeanParam.getType()!=null) { 
						cell.setCellValue(this.getXSSFRichTextString(salaryBean.getBeAttendanceDaysCor(),salaryBean.getBeAttendanceDays(),redZtFont,ztFont));
					}else{
						if (salaryBean.getBeAttendanceDaysCor() != null) {
							cell.setCellValue(salaryBean.getBeAttendanceDaysCor());
						} else if (salaryBean.getBeAttendanceDays() != null) {
							cell.setCellValue(salaryBean.getBeAttendanceDays());
						}
					}
				} else {
//					cell.setCellValue("");
				}
				//迟到次数
				cell = row.createCell(13);
				cell.setCellStyle(ztStyle);
				if (salaryBean.getLateCountCor() != null || salaryBean.getLateCount() != null) {
					if(salaryBeanParam.getType()!=null) { 
						cell.setCellValue(this.getXSSFRichTextString(salaryBean.getLateCountCor(),salaryBean.getLateCount(),redZtFont,ztFont));
					}else {
						if (salaryBean.getLateCountCor() != null) {
							cell.setCellValue(salaryBean.getLateCountCor());
						} else if (salaryBean.getLateCount() != null) {
							cell.setCellValue(salaryBean.getLateCount());
						}
					}
				} else {
//					cell.setCellValue("");
				}

				//迟到扣款
				int nums = 13;
				nums++;
				cell = row.createCell(nums);
				cell.setCellStyle(ztStyle);
				if (salaryBean.getLateMoneyCor() != null || salaryBean.getLateMoney() != null) {
					if(salaryBeanParam.getType()!=null) { 
						cell.setCellValue(this.getXSSFRichTextString(salaryBean.getLateMoneyCor(),salaryBean.getLateMoney(),redZtFont,ztFont));
					}else {
						if (salaryBean.getLateMoneyCor() != null) {
							cell.setCellValue(salaryBean.getLateMoneyCor());
						} else if (salaryBean.getLateMoney() != null) {
							cell.setCellValue(salaryBean.getLateMoney());
						}
					}
				} else {
//					cell.setCellValue("");
				}
				
				nums++;
				cell = row.createCell(nums);
				cell.setCellStyle(ztStyle);
				if (salaryBean.getLeaveEarlyCountCor() != null || salaryBean.getLeaveEarlyCount() != null) {
					if(salaryBeanParam.getType()!=null) { 
						cell.setCellValue(this.getXSSFRichTextString(salaryBean.getLeaveEarlyCountCor(),salaryBean.getLeaveEarlyCount(),redZtFont,ztFont));
					}else {
						if (salaryBean.getLeaveEarlyCountCor() != null) {
							cell.setCellValue(salaryBean.getLeaveEarlyCountCor());
						} else if (salaryBean.getLeaveEarlyCount() != null) {
							cell.setCellValue(salaryBean.getLeaveEarlyCount());
						}
					}
				} else {
//					cell.setCellValue("");
				}
				
				//早退扣款
				nums++;
				cell = row.createCell(nums);
				cell.setCellStyle(ztStyle);
				if (salaryBean.getLeaveEarlyMoneyCor() != null || salaryBean.getLeaveEarlyMoney() != null) {
					if(salaryBeanParam.getType()!=null) { 
						cell.setCellValue(this.getXSSFRichTextString(salaryBean.getLeaveEarlyMoneyCor(),salaryBean.getLeaveEarlyMoney(),redZtFont,ztFont));
					}else {
						if (salaryBean.getLeaveEarlyMoneyCor() != null) {
							cell.setCellValue(salaryBean.getLeaveEarlyMoneyCor());
						} else if (salaryBean.getLeaveEarlyMoney() != null) {
							cell.setCellValue(salaryBean.getLeaveEarlyMoney());
						}
					}
				} else {
//					cell.setCellValue("");
				}
				//旷工
				nums++;
				cell = row.createCell(nums);
				cell.setCellStyle(ztStyle);
				if (salaryBean.getAbsentCountCor() != null || salaryBean.getAbsentCount() != null) {
					if(salaryBeanParam.getType()!=null) { 
						cell.setCellValue(this.getXSSFRichTextString(salaryBean.getAbsentCountCor(),salaryBean.getAbsentCount(),redZtFont,ztFont));
					}else {
						if (salaryBean.getAbsentCountCor() != null) {
							cell.setCellValue(salaryBean.getAbsentCountCor());
						} else if (salaryBean.getAbsentCount() != null) {
							cell.setCellValue(salaryBean.getAbsentCount());
						}
					}
				} else {
//					cell.setCellValue("");
				}
				
				//补签
				nums++;
				cell = row.createCell(nums);
				cell.setCellStyle(ztStyle);
				if (salaryBean.getRetroactiveCountCor() != null || salaryBean.getRetroactiveCount() != null) {
					if(salaryBeanParam.getType()!=null) { 
						cell.setCellValue(this.getXSSFRichTextString(salaryBean.getRetroactiveCountCor(),salaryBean.getRetroactiveCount(),redZtFont,ztFont));
					}else {
						if (salaryBean.getRetroactiveCountCor() != null) {
							cell.setCellValue(salaryBean.getRetroactiveCountCor());
						} else if (salaryBean.getRetroactiveCount() != null) {
							cell.setCellValue(salaryBean.getRetroactiveCount());
						}
					}
				} else {
//					cell.setCellValue("");
				}
				
				//平时加班
				nums++;
				cell = row.createCell(nums);
				cell.setCellStyle(ztStyle);
				if (salaryBean.getOvertimeNomalCor() != null || salaryBean.getOvertimeNomal() != null) {
					if(salaryBeanParam.getType()!=null) { 
						cell.setCellValue(this.getXSSFRichTextString(salaryBean.getOvertimeNomalCor(),salaryBean.getOvertimeNomal(),redZtFont,ztFont));
					}else {
						if (salaryBean.getOvertimeNomalCor() != null) {
							cell.setCellValue(salaryBean.getOvertimeNomalCor());
						} else if (salaryBean.getOvertimeNomal() != null) {
							cell.setCellValue(salaryBean.getOvertimeNomal());
						}
					}
				} else {
//					cell.setCellValue("");
				}
				
				//周末加班
				nums++;
				cell = row.createCell(nums);
				cell.setCellStyle(ztStyle);
				if (salaryBean.getOvertimeWeekendCor() != null || salaryBean.getOvertimeWeekend() != null) {
					if(salaryBeanParam.getType()!=null) {
						cell.setCellValue(this.getXSSFRichTextString(salaryBean.getOvertimeWeekendCor(),salaryBean.getOvertimeWeekend(),redZtFont,ztFont));
					}else {
						if (salaryBean.getOvertimeWeekendCor() != null) {
							cell.setCellValue(salaryBean.getOvertimeWeekendCor());
						} else if (salaryBean.getOvertimeWeekend() != null) {
							cell.setCellValue(salaryBean.getOvertimeWeekend());
						}
					}
				} else {
//					cell.setCellValue("");
				}
				
				//节日加班
				nums++;
				cell = row.createCell(nums);
				cell.setCellStyle(ztStyle);
				if (salaryBean.getOvertimeHolidayCor() != null || salaryBean.getOvertimeHoliday() != null) {
					if(salaryBeanParam.getType()!=null) {
						cell.setCellValue(this.getXSSFRichTextString(salaryBean.getOvertimeHolidayCor(),salaryBean.getOvertimeHoliday(),redZtFont,ztFont));
					}else {
						if (salaryBean.getOvertimeHolidayCor() != null) {
							cell.setCellValue(salaryBean.getOvertimeHolidayCor());
						} else if (salaryBean.getOvertimeHoliday() != null) {
							cell.setCellValue(salaryBean.getOvertimeHoliday());
						}
					}
				} else {
//					cell.setCellValue("");
				}
				//年假
				nums++;
				cell = row.createCell(nums);
				cell.setCellStyle(ztStyle);
				if (salaryBean.getRestYearUsedCor() != null || salaryBean.getRestYearUsed() != null) {
					if(salaryBeanParam.getType()!=null) { 
						cell.setCellValue(this.getXSSFRichTextString(salaryBean.getRestYearUsedCor(),salaryBean.getRestYearUsed(),redZtFont,ztFont));
					}else {
						if (salaryBean.getRestYearUsedCor() != null) {
							cell.setCellValue(salaryBean.getRestYearUsedCor());
						} else if (salaryBean.getRestYearUsed() != null) {
							cell.setCellValue(salaryBean.getRestYearUsed());
						}
					}
				} else {
//					cell.setCellValue("");
				}
				
				//调休
				nums++;
				cell = row.createCell(nums);
				cell.setCellStyle(ztStyle);
				if (salaryBean.getRestTuneUsedCor() != null || salaryBean.getRestTuneUsed() != null) {
					if(salaryBeanParam.getType()!=null) {
						cell.setCellValue(this.getXSSFRichTextString(salaryBean.getRestTuneUsedCor(),salaryBean.getRestTuneUsed(),redZtFont,ztFont));
					}else {
						if (salaryBean.getRestTuneUsedCor() != null) {
							cell.setCellValue(salaryBean.getRestTuneUsedCor());
						} else if (salaryBean.getRestTuneUsed() != null) {
							cell.setCellValue(salaryBean.getRestTuneUsed());
						}
					}
				} else {
//					cell.setCellValue("");
				}
				
				//产假
				nums++;
				cell = row.createCell(nums);
				cell.setCellStyle(ztStyle);
				if (salaryBean.getRestBornUsedCor() != null || salaryBean.getRestBornUsed() != null) {
					if(salaryBeanParam.getType()!=null) { 
						cell.setCellValue(this.getXSSFRichTextString(salaryBean.getRestBornUsedCor(),salaryBean.getRestBornUsed(),redZtFont,ztFont));
					}else {
						if (salaryBean.getRestBornUsedCor() != null) {
							cell.setCellValue(salaryBean.getRestBornUsedCor());
						} else if (salaryBean.getRestBornUsed() != null) {
							cell.setCellValue(salaryBean.getRestBornUsed());
						}
					}
				} else {
//					cell.setCellValue("");
				}
				
				//产检
				nums++;
				cell = row.createCell(nums);
				cell.setCellStyle(ztStyle);
				if (salaryBean.getRestCheckUsedCor() != null || salaryBean.getRestCheckUsed() != null) {
					if(salaryBeanParam.getType()!=null) { 
						cell.setCellValue(this.getXSSFRichTextString(salaryBean.getRestCheckUsedCor(),salaryBean.getRestCheckUsed(),redZtFont,ztFont));
					}else {
						if (salaryBean.getRestCheckUsedCor() != null) {
							cell.setCellValue(salaryBean.getRestCheckUsedCor());
						} else if (salaryBean.getRestCheckUsed() != null) {
							cell.setCellValue(salaryBean.getRestCheckUsed());
						}
					}
				} else {
//					cell.setCellValue("");
				}
				
				//丧假
				nums++;
				cell = row.createCell(nums);
				cell.setCellStyle(ztStyle);
				if (salaryBean.getRestDieUsedCor() != null || salaryBean.getRestDieUsed() != null) {
					if(salaryBeanParam.getType()!=null) { 
						cell.setCellValue(this.getXSSFRichTextString(salaryBean.getRestDieUsedCor(),salaryBean.getRestDieUsed(),redZtFont,ztFont));
					}else {
						if (salaryBean.getRestDieUsedCor() != null) {
							cell.setCellValue(salaryBean.getRestDieUsedCor());
						} else if (salaryBean.getRestDieUsed() != null) {
							cell.setCellValue(salaryBean.getRestDieUsed());
						}
					}
				} else {
//					cell.setCellValue("");
				}
				
				//婚假
				nums++;
				cell = row.createCell(nums);
				cell.setCellStyle(ztStyle);
				if (salaryBean.getRestMarryUsedCor() != null || salaryBean.getRestMarryUsed() != null) {
					if(salaryBeanParam.getType()!=null) { 
						cell.setCellValue(this.getXSSFRichTextString(salaryBean.getRestMarryUsedCor(),salaryBean.getRestMarryUsed(),redZtFont,ztFont));
					}else {
						if (salaryBean.getRestMarryUsedCor() != null) {
							cell.setCellValue(salaryBean.getRestMarryUsedCor());
						} else if (salaryBean.getRestMarryUsed() != null) {
							cell.setCellValue(salaryBean.getRestMarryUsed());
						}
					}
				} else {
//					cell.setCellValue("");
				}
				
				//陪护假
				nums++;
				cell = row.createCell(nums);
				cell.setCellStyle(ztStyle);
				if (salaryBean.getRestBornAttendUsedCor() != null || salaryBean.getRestBornAttendUsed() != null) {
					if(salaryBeanParam.getType()!=null) {
						cell.setCellValue(this.getXSSFRichTextString(salaryBean.getRestBornAttendUsedCor(),salaryBean.getRestBornAttendUsed(),redZtFont,ztFont));
					}else {
						if (salaryBean.getRestBornAttendUsedCor() != null) {
							cell.setCellValue(salaryBean.getRestBornAttendUsedCor());
						} else if (salaryBean.getRestBornAttendUsed() != null) {
							cell.setCellValue(salaryBean.getRestBornAttendUsed());
						}
					}
				} else {
//					cell.setCellValue("");
				}
				
				//哺乳假
				nums++;
				cell = row.createCell(nums);
				cell.setCellStyle(ztStyle);
				if (salaryBean.getRestSuckleUsedCor() != null || salaryBean.getRestSuckleUsed() != null) {
					if(salaryBeanParam.getType()!=null) { 
						cell.setCellValue(this.getXSSFRichTextString(salaryBean.getRestSuckleUsedCor(),salaryBean.getRestSuckleUsed(),redZtFont,ztFont));
					}else {
						if (salaryBean.getRestSuckleUsedCor() != null) {
							cell.setCellValue(salaryBean.getRestSuckleUsedCor());
						} else if (salaryBean.getRestSuckleUsed() != null) {
							cell.setCellValue(salaryBean.getRestSuckleUsed());
						}
					}
				} else {
//					cell.setCellValue("");
				}
				
				//病假
				nums++;
				cell = row.createCell(nums);
				cell.setCellStyle(ztStyle);
				if (salaryBean.getRestIllUsedCor() != null || salaryBean.getRestIllUsed() != null) {
					if(salaryBeanParam.getType()!=null) { 
						cell.setCellValue(this.getXSSFRichTextString(salaryBean.getRestIllUsedCor(),salaryBean.getRestIllUsed(),redZtFont,ztFont));
					}else {
						if (salaryBean.getRestIllUsedCor() != null) {
							cell.setCellValue(salaryBean.getRestIllUsedCor());
						} else if (salaryBean.getRestIllUsed() != null) {
							cell.setCellValue(salaryBean.getRestIllUsed());
						}
					}
				} else {
//					cell.setCellValue("");
				}
				
				//事假
				nums++;
				cell = row.createCell(nums);
				cell.setCellStyle(ztStyle);
				if (salaryBean.getRestThingUsedCor() != null || salaryBean.getRestThingUsed() != null) {
					if(salaryBeanParam.getType()!=null) { 
						cell.setCellValue(this.getXSSFRichTextString(salaryBean.getRestThingUsedCor(),salaryBean.getRestThingUsed(),redZtFont,ztFont));
					}else {
						if (salaryBean.getRestThingUsedCor() != null) {
							cell.setCellValue(salaryBean.getRestThingUsedCor());
						} else if (salaryBean.getRestThingUsed() != null) {
							cell.setCellValue(salaryBean.getRestThingUsed());
						}
					}
				} else {
//					cell.setCellValue("");
				}
				//备注
				nums++;
				cell = row.createCell(nums);
				cell.setCellStyle(ztStyle);
				if (salaryBean.getDescription() != null) {
					cell.setCellValue(salaryBean.getDescription());
				} else {
//					cell.setCellValue("");
				}
			}
			
			try {
				FileOutputStream outStream = new FileOutputStream(fileurl);
				wb.write(outStream);
				inputStream.close();
				outStream.close();
			} catch (Exception e) {
				return null;
			}
			return url;
	}
	
	/**
	 * 
	 * @param daysCor
	 * @param days
	 * @param redZtFont
	 * @param ztFont
	 * @return
	 */
	@SuppressWarnings("unused")
	private XSSFRichTextString getXSSFRichTextString(Object daysCor,Object days,Font redZtFont,Font ztFont) {
		String str=null;
		if (daysCor!= null) {
			str=daysCor.toString();
		}
		if (days!= null) {
			if(str!=null) {
				if(!daysCor.equals(days)) {			
					str=str+" ("+days.toString()+")";
				}else {
					str=days.toString();
				}
			}else {
				str=days.toString();
			}
		}
		XSSFRichTextString ts= new XSSFRichTextString(str);
		if(str.contains("(")) {
			ts.applyFont(0,daysCor.toString().length(),redZtFont);
			ts.applyFont(daysCor.toString().length()+1,str.length()-1,ztFont);
		}else {
			if(daysCor!= null) {
				ts.applyFont(0,str.length(),redZtFont);
			}
			if(days!= null) {
				ts.applyFont(0,str.length(),ztFont);
			}
		}
		return ts;
	}

	/**
	 * 查询提交明细
	 */
	@Override
	public List<SalaryBean> getSalaryListDetail(Integer salaryRequestId) {
		SalaryExample salaryExample = new SalaryExample();
		salaryExample.createCriteria().andSalaryRequestIdEqualTo(salaryRequestId);
		
		List<Salary> salaryList = salaryDao.selectByExample(salaryExample);
		List<SalaryBean> salaryBeanList = new ArrayList<>();
		for (Salary salary : salaryList) {
			SalaryBean salaryBean = new SalaryBean();
			salaryBean.setAbsentCount(salary.getAbsentCount());
			salaryBean.setAbsentCountCor(salary.getAbsentCount());
			salaryBean.setAbsentCount(salary.getAbsentCount());
			salaryBean.setAbsentCountCor(salary.getAbsentCountCor());
			salaryBean.setBeAttendanceDays(salary.getBeAttendanceDays());
			salaryBean.setBeAttendanceDaysCor(salary.getBeAttendanceDaysCor());
			salaryBean.setChineseName(salary.getChineseName());
			salaryBean.setDeptmentNameFour(salary.getDeptmentNameFour());
			salaryBean.setDeptmentNameOne(salary.getDeptmentNameOne());
			salaryBean.setDeptmentNameThree(salary.getDeptmentNameThree());
			salaryBean.setDeptmentNameTwo(salary.getDeptmentNameTwo());
			salaryBean.setDescription(salary.getDescription());
			salaryBean.setEmployeeNo(salary.getEmployeeNo());
			salaryBean.setEmployeeNoSubmit(salary.getEmployeeNoSubmit());//1
			salaryBean.setEmployeeSeparatedDate(salary.getEmployeeSeparatedDate());
			salaryBean.setEndDate(salary.getEndDate());//6
			salaryBean.setEnglishName(salary.getEnglishName());
			salaryBean.setLateCount(salary.getLateCount());
			salaryBean.setLateCountCor(salary.getLateCountCor());
			salaryBean.setLateMoney(salary.getLateMoney());
			salaryBean.setLateMoneyCor(salary.getLateMoneyCor());
			salaryBean.setLeaveEarlyCount(salary.getLeaveEarlyCount());
			salaryBean.setLeaveEarlyCountCor(salary.getLeaveEarlyCountCor());
			salaryBean.setLeaveEarlyMoney(salary.getLeaveEarlyMoney());
			salaryBean.setLeaveEarlyMoneyCor(salary.getLeaveEarlyMoneyCor());
			salaryBean.setOvertimeHoliday(salary.getOvertimeHoliday());
			salaryBean.setOvertimeHolidayCor(salary.getOvertimeHolidayCor());
			salaryBean.setOvertimeNomal(salary.getOvertimeNomal());
			salaryBean.setOvertimeNomalCor(salary.getOvertimeNomalCor());
			salaryBean.setOvertimeWeekend(salary.getOvertimeWeekend());
			salaryBean.setOvertimeWeekendCor(salary.getOvertimeWeekendCor());
			salaryBean.setRestBornAttendUsed(salary.getRestBornAttendUsed());
			salaryBean.setRestBornAttendUsedCor(salary.getRestBornAttendUsedCor());
			salaryBean.setRestBornUsed(salary.getRestBornUsed());
			salaryBean.setRestBornUsedCor(salary.getRestBornUsedCor());
			salaryBean.setRestCheckUsed(salary.getRestCheckUsed());
			salaryBean.setRestCheckUsedCor(salary.getRestCheckUsedCor());
			salaryBean.setRestDieUsed(salary.getRestDieUsed());
			salaryBean.setRestDieUsedCor(salary.getRestDieUsedCor());
			salaryBean.setRestIllUsed(salary.getRestIllUsed());
			salaryBean.setRestIllUsedCor(salary.getRestIllUsedCor());
			salaryBean.setRestMarryUsed(salary.getRestMarryUsed());
			salaryBean.setRestMarryUsedCor(salary.getRestMarryUsedCor());
			salaryBean.setRestSuckleUsed(salary.getRestSuckleUsed());
			salaryBean.setRestSuckleUsedCor(salary.getRestSuckleUsedCor());
			salaryBean.setRestThingUsed(salary.getRestThingUsed());
			salaryBean.setRestThingUsedCor(salary.getRestThingUsedCor());
			salaryBean.setRestTuneUsed(salary.getRestTuneUsed());
			salaryBean.setRestTuneUsedCor(salary.getRestTuneUsedCor());
			salaryBean.setRestYearUsed(salary.getRestYearUsed());
			salaryBean.setRestYearUsedCor(salary.getRestYearUsedCor());
			salaryBean.setRetroactiveCount(salary.getRetroactiveCount());
			salaryBean.setRetroactiveCountCor(salary.getRetroactiveCountCor());
			salaryBean.setRoleName(salary.getRoleName());
			salaryBean.setSalaryTempId(salary.getSalaryTempId());//2
			salaryBean.setStartDate(salary.getStartDate());//3
			salaryBean.setLevel(salary.getLevel());
			salaryBean.setSex(salary.getSex());
			salaryBeanList.add(salaryBean);
		}
		return salaryBeanList;
	}

	/**
	 * 取得excel中的年月
	 */
	@Override
	public SalaryListBean getExcelYearMonth(Integer attachmentId, Integer employeeId) {
		// 查询附件表获取路径地址
		Attachment attachment = attachmentDao.selectByPrimaryKey(attachmentId);
		// 设置文件路径
		String pathName = attachment.getUrl().replace(AppConstant.UPLOAD_RESOURCE, AppConstant.SAVE_FILE_REPOSITORY);
		File file = new File(pathName);
		Workbook work = null;
		if (file.exists()) {
			try {
				InputStream inputStream = new FileInputStream(file);
				if (pathName.matches("^.+\\.(?i)(xls)$")) {
					work = new HSSFWorkbook(inputStream);
				} else if (pathName.matches("^.+\\.(?i)(xlsx)$")) {
					work = new XSSFWorkbook(inputStream);
				}
				if (inputStream != null) {
					inputStream.close();
				}
			} catch (Exception ex) {
				throw new ResultFaultException("0_00003");
			}
		} else {
			throw new ResultFaultException("0_00003");
		}
		if (work == null) {
			throw new ResultFaultException("0_00003");
		}

		if (work != null) {
			//获取工作表的数量
			int sheetNum = work.getNumberOfSheets();
			//遍历工作表 找出要导入的Excel表
			for (int m = 0; m < sheetNum; m++) {
				Sheet sh = work.getSheetAt(m);
				if (sh.getSheetName().equals("考勤报表（工资使用）")) {
					Sheet sheet = work.getSheetAt(m);
					List<String> errorMsgList = new ArrayList<String>();// 创建错误信息列表
					//获取报表的年月
					Date endDate = new Date();
					SalaryListBean salaryListBean = new SalaryListBean();
					
					Row rowDate = sheet.getRow(0);

					SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
					//结束时间
					if (rowDate.getCell(8) != null && StringUtils.isNotBlank(rowDate.getCell(8).toString())) {
						try {//如果是日期格式
							endDate = rowDate.getCell(8).getDateCellValue();
						} catch (Exception e) {
							String endDateStr = rowDate.getCell(8).getStringCellValue();
							try {//如果是字符串格式
								endDate = sdf.parse(endDateStr);
							} catch (ParseException e1) {
								e1.printStackTrace();
								errorMsgList.add("第1行，结束时间格式不正确。");
							}
						}
						if (endDate != null) {
						} else {
							errorMsgList.add("第1行，结束时间格式不正确。");
						}
					} else {
						errorMsgList.add("第1行，结束时间不能为空。");
					}
					if (errorMsgList.size() > 0) {
						throw new ResultFaultException("8_00002", errorMsgList);
					}
					//获取年份月份
					Calendar calendar = Calendar.getInstance();
			        calendar.setTime(endDate);
			        salaryListBean.setSalaryYear(calendar.get(Calendar.YEAR));
			        salaryListBean.setSalaryMonth(calendar.get(Calendar.MONTH)+1);
				
					return salaryListBean;
				}
			}
		}
		throw new ResultFaultException("0_00014");
	}

	/**
	 * 删除工资报表，根据开始日期，结束日期，人员no
	 */
	@Override
	public boolean deleteSalary(SalaryBean salaryBean) {
		Employee emp = employeeDao.selectByPrimaryKey(salaryBean.getEmployeeId());
		String employeeNoSubmit = emp.getEmployeeNo();
		
		//判断是否有申请的信息
		SalaryExample salaryExample = new SalaryExample();
		salaryExample.createCriteria().andStartDateEqualTo(salaryBean.getHistoryStartDate())
		.andEndDateEqualTo(salaryBean.getHistoryEndDate())
		.andEmployeeNoSubmitEqualTo(employeeNoSubmit)
		.andRequestStateNotEqualTo(0);
		
		List<Salary> salaryList = salaryDao.selectByExample(salaryExample);
		if (salaryList.size()>0) {
			throw new ResultFaultException("16_00011");
		}
		SalaryExample delExample = new SalaryExample();
		delExample.createCriteria().andStartDateEqualTo(salaryBean.getHistoryStartDate())
		.andEndDateEqualTo(salaryBean.getHistoryEndDate())
		.andEmployeeNoSubmitEqualTo(employeeNoSubmit);
		int record = salaryDao.deleteByExample(delExample);
		if (record<=0) {
			return false;
		}
		return true;
	}
	
	/**
	 * 比较修改值
	 * @param salaryBean
	 * @param salary
	 * @param errorMsgList
	 * @param j
	 * @return
	 */
	private List<String> compareValue(SalaryBean salaryBean, Salary salary, List<String> errorMsgList, int j) {
		boolean isLackDesc = false;
		//应出勤
		if ((salary.getBeAttendanceDays()==null && salaryBean.getBeAttendanceDaysCor()!=null)
				|| (salary.getBeAttendanceDays()!=null && salaryBean.getBeAttendanceDaysCor()!=null
						&& !salary.getBeAttendanceDays().equals(salaryBean.getBeAttendanceDaysCor()))) {
			isLackDesc = true;
		}
		if ((salary.getBeAttendanceDays()!=null && salaryBean.getBeAttendanceDaysCor()==null)) {
			isLackDesc = true;
			salaryBean.setBeAttendanceDaysCor(0);
		}
		//迟到
		if ((salary.getLateCount()==null && salaryBean.getLateCountCor()!=null)
				|| (salary.getLateCount()!=null && salaryBean.getLateCountCor()!=null
						&& !salary.getLateCount().equals(salaryBean.getLateCountCor()))) {
			isLackDesc = true;
		}
		if ((salary.getLateCount()!=null && salaryBean.getLateCountCor()==null)) {
			isLackDesc = true;
			salaryBean.setLateCountCor(0);
		}
		//早退
		if ((salary.getLeaveEarlyCount()==null && salaryBean.getLeaveEarlyCountCor()!=null)
				|| (salary.getLeaveEarlyCount()!=null && salaryBean.getLeaveEarlyCountCor()!=null
						&& !salary.getLeaveEarlyCount().equals(salaryBean.getLeaveEarlyCountCor()))) {
			isLackDesc = true;
		}
		if ((salary.getLeaveEarlyCount()!=null && salaryBean.getLeaveEarlyCountCor()==null)) {
			isLackDesc = true;
			salaryBean.setLeaveEarlyCountCor(0);
		}
		//旷工
		if ((salary.getAbsentCount()==null && salaryBean.getAbsentCountCor()!=null)
				|| (salary.getAbsentCount()!=null && salaryBean.getAbsentCountCor()!=null
						&& !salary.getAbsentCount().equals(salaryBean.getAbsentCountCor()))) {
			isLackDesc = true;
		}
		if ((salary.getAbsentCount()!=null && salaryBean.getAbsentCountCor()==null)) {
			isLackDesc = true;
			salaryBean.setAbsentCountCor(0);
		}
		//补签
		if ((salary.getRetroactiveCount()==null && salaryBean.getRetroactiveCountCor()!=null)
				|| (salary.getRetroactiveCount()!=null && salaryBean.getRetroactiveCountCor()!=null
						&& !salary.getRetroactiveCount().equals(salaryBean.getRetroactiveCountCor()))) {
			isLackDesc = true;
		}
		if ((salary.getRetroactiveCount()!=null && salaryBean.getRetroactiveCountCor()==null)) {
			isLackDesc = true;
			salaryBean.setRetroactiveCountCor(0);
		}
		//平时加班
		if ((salary.getOvertimeNomal()==null && salaryBean.getOvertimeNomalCor()!=null)
				|| (salary.getOvertimeNomal()!=null && salaryBean.getOvertimeNomalCor()!=null
						&& !salary.getOvertimeNomal().equals(salaryBean.getOvertimeNomalCor()))) {
			isLackDesc = true;
		}
		if ((salary.getOvertimeNomal()!=null && salaryBean.getOvertimeNomalCor()==null)) {
			isLackDesc = true;
			salaryBean.setOvertimeNomalCor(new Float(0));
		}
		//双休加班
		if ((salary.getOvertimeWeekend()==null && salaryBean.getOvertimeWeekendCor()!=null)
				|| (salary.getOvertimeWeekend()!=null && salaryBean.getOvertimeWeekendCor()!=null
						&& !salary.getOvertimeWeekend().equals(salaryBean.getOvertimeWeekendCor()))) {
			isLackDesc = true;
		}
		if ((salary.getOvertimeWeekend()!=null && salaryBean.getOvertimeWeekendCor()==null)) {
			isLackDesc = true;
			salaryBean.setOvertimeWeekendCor(new Float(0));
		}
		//节日加班
		if ((salary.getOvertimeHoliday()==null && salaryBean.getOvertimeHolidayCor()!=null)
				|| (salary.getOvertimeHoliday()!=null && salaryBean.getOvertimeHolidayCor()!=null
						&& !salary.getOvertimeHoliday().equals(salaryBean.getOvertimeHolidayCor()))) {
			isLackDesc = true;
		}
		if ((salary.getOvertimeHoliday()!=null && salaryBean.getOvertimeHolidayCor()==null)) {
			isLackDesc = true;
			salaryBean.setOvertimeHolidayCor(new Float(0));
		}
		//年假
		if ((salary.getRestYearUsed()==null && salaryBean.getRestYearUsedCor()!=null)
				|| (salary.getRestYearUsed()!=null && salaryBean.getRestYearUsedCor()!=null
						&& !salary.getRestYearUsed().equals(salaryBean.getRestYearUsedCor()))) {
			isLackDesc = true;
		}
		if ((salary.getRestYearUsed()!=null && salaryBean.getRestYearUsedCor()==null)) {
			isLackDesc = true;
			salaryBean.setRestYearUsedCor(new Float(0));
		}
		//调休
		if ((salary.getRestTuneUsed()==null && salaryBean.getRestTuneUsedCor()!=null)
				|| (salary.getRestTuneUsed()!=null && salaryBean.getRestTuneUsedCor()!=null
						&& !salary.getRestTuneUsed().equals(salaryBean.getRestTuneUsedCor()))) {
			isLackDesc = true;
		}
		if ((salary.getRestTuneUsed()!=null && salaryBean.getRestTuneUsedCor()==null)) {
			isLackDesc = true;
			salaryBean.setRestTuneUsedCor(new Float(0));
		}
		//产假
		if ((salary.getRestBornUsed()==null && salaryBean.getRestBornUsedCor()!=null)
				|| (salary.getRestBornUsed()!=null && salaryBean.getRestBornUsedCor()!=null
						&& !salary.getRestBornUsed().equals(salaryBean.getRestBornUsedCor()))) {
			isLackDesc = true;
		}
		if ((salary.getRestBornUsed()!=null && salaryBean.getRestBornUsedCor()==null)) {
			isLackDesc = true;
			salaryBean.setRestBornUsedCor(0);
		}
		//产检
		if ((salary.getRestCheckUsed()==null && salaryBean.getRestCheckUsedCor()!=null)
				|| (salary.getRestCheckUsed()!=null && salaryBean.getRestCheckUsedCor()!=null
						&& !salary.getRestCheckUsed().equals(salaryBean.getRestCheckUsedCor()))) {
			isLackDesc = true;
		}
		if ((salary.getRestCheckUsed()!=null && salaryBean.getRestCheckUsedCor()==null)) {
			isLackDesc = true;
			salaryBean.setRestCheckUsedCor(new Float(0));
		}
		//丧假
		if ((salary.getRestDieUsed()==null && salaryBean.getRestDieUsedCor()!=null)
				|| (salary.getRestDieUsed()!=null && salaryBean.getRestDieUsedCor()!=null
						&& !salary.getRestDieUsed().equals(salaryBean.getRestDieUsedCor()))) {
			isLackDesc = true;
		}
		if ((salary.getRestDieUsed()!=null && salaryBean.getRestDieUsedCor()==null)) {
			isLackDesc = true;
			salaryBean.setRestDieUsedCor(0);
		}
		//婚假
		if ((salary.getRestMarryUsed()==null && salaryBean.getRestMarryUsedCor()!=null)
				|| (salary.getRestMarryUsed()!=null && salaryBean.getRestMarryUsedCor()!=null
						&& !salary.getRestMarryUsed().equals(salaryBean.getRestMarryUsedCor()))) {
			isLackDesc = true;
		}
		if ((salary.getRestMarryUsed()!=null && salaryBean.getRestMarryUsedCor()==null)) {
			isLackDesc = true;
			salaryBean.setRestMarryUsedCor(0);
		}
		//陪护假
		if ((salary.getRestBornAttendUsed()==null && salaryBean.getRestBornAttendUsedCor()!=null)
				|| (salary.getRestBornAttendUsed()!=null && salaryBean.getRestBornAttendUsedCor()!=null
						&& !salary.getRestBornAttendUsed().equals(salaryBean.getRestBornAttendUsedCor()))) {
			isLackDesc = true;
		}
		if ((salary.getRestBornAttendUsed()!=null && salaryBean.getRestBornAttendUsedCor()==null)) {
			isLackDesc = true;
			salaryBean.setRestBornAttendUsedCor(0);
		}
		//哺乳假
		if ((salary.getRestSuckleUsed()==null && salaryBean.getRestSuckleUsedCor()!=null)
				|| (salary.getRestSuckleUsed()!=null && salaryBean.getRestSuckleUsedCor()!=null
						&& !salary.getRestSuckleUsed().equals(salaryBean.getRestSuckleUsedCor()))) {
			isLackDesc = true;
		}
		if ((salary.getRestSuckleUsed()!=null && salaryBean.getRestSuckleUsedCor()==null)) {
			isLackDesc = true;
			salaryBean.setRestSuckleUsedCor(new Float(0));
		}
		//病假
		if ((salary.getRestIllUsed()==null && salaryBean.getRestIllUsedCor()!=null)
				|| (salary.getRestIllUsed()!=null && salaryBean.getRestIllUsedCor()!=null
						&& !salary.getRestIllUsed().equals(salaryBean.getRestIllUsedCor()))) {
			isLackDesc = true;
		}
		if ((salary.getRestIllUsed()!=null && salaryBean.getRestIllUsedCor()==null)) {
			isLackDesc = true;
			salaryBean.setRestIllUsedCor(new Float(0));
		}
		//事假
		if ((salary.getRestThingUsed()==null && salaryBean.getRestThingUsedCor()!=null)
				|| (salary.getRestThingUsed()!=null && salaryBean.getRestThingUsedCor()!=null
						&& !salary.getRestThingUsed().equals(salaryBean.getRestThingUsedCor()))) {
			isLackDesc = true;
		}
		if ((salary.getRestThingUsed()!=null && salaryBean.getRestThingUsedCor()==null)) {
			isLackDesc = true;
			salaryBean.setRestThingUsedCor(new Float(0));
		}
		//迟到扣款
		if (salaryBean.getLateMoneyCor()!=null) {
			isLackDesc = true;
		}
		//早退扣款
		if (salaryBean.getLeaveEarlyMoneyCor()!=null) {
			isLackDesc = true;
		}
		if (isLackDesc) {
			errorMsgList.add("第"+(j+4)+"行，"+ salary.getChineseName()+"("+salary.getEmployeeNo()+")");
		}
		
		return errorMsgList;
	}

	/**
	 * 查询工资汇总表
	 * by年份，月份
	 */
	@Override
	public SalaryPageBean getSalaryTotalList(SalaryBean salaryBean) {
		//筛选条件转换（code转name）查出公司name集合，部门name集合
		salaryBean = this.getSalaryBeanPropety(salaryBean);
		//从主数据表查
		List<SalaryBean> salaryList = salaryExtDao.getSalaryTotalList(salaryBean);
		int count = salaryExtDao.countTotal(salaryBean);
		
		SalaryPageBean salaryPageBean = new SalaryPageBean();
		salaryPageBean.setPageTotal(count);
		salaryPageBean.setSalaryBeanList(salaryList);
		return salaryPageBean;
	}
}
